package com.chaos.view.example;

import com.chaos.view.LogUtil;
import com.chaos.view.PinView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Checkbox;
import ohos.agp.components.ComponentState;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.StateElement;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.IOException;
import java.io.InputStream;

public class SecondAbilitySlice extends AbilitySlice {
    private static final int CONTENT_SHOW_MODE = 2;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_second);
    }

    @Override
    public void onActive() {
        super.onActive();
        Checkbox checkPin = (Checkbox) findComponentById(ResourceTable.Id_check_pin);
        Checkbox checkPassWord = (Checkbox) findComponentById(ResourceTable.Id_check_pass_word);
        checkBoxAddStatus(ResourceTable.Media_check_yes, ResourceTable.Media_check_no, this, checkPin);
        checkBoxAddStatus(ResourceTable.Media_check_yes, ResourceTable.Media_check_no, this, checkPassWord);
        PinView pinview6 = (PinView) findComponentById(ResourceTable.Id_pinView6);
        PinView pinview7 = (PinView) findComponentById(ResourceTable.Id_pinView7);
        checkPin.setCheckedStateChangedListener((absButton, b) -> {
            if (b) {
                pinview6.setContentShowMode(1);
            } else {
                pinview6.setContentShowMode(CONTENT_SHOW_MODE);
            }
        });
        checkPassWord.setCheckedStateChangedListener((absButton, b) -> {
            if (b) {
                pinview7.setContentShowMode(1);
            } else {
                pinview7.setContentShowMode(CONTENT_SHOW_MODE);
            }
        });
        pinview7.setContentShowMode(1);
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * 通过资源ID获取位图对象
     *
     * @param drawableId drawableId
     * @param context context
     * @return PixelMap
     **/
    private static PixelMap getPixelMap(int drawableId, Context context) {
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = context.getResourceManager().getResource(drawableId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(0, 0);
            decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;

            return imageSource.createPixelmap(decodingOptions);
        } catch (IOException | NotExistException e) {
            LogUtil.d(e.getMessage());
        } finally {
            try {
                if (drawableInputStream != null) {
                    drawableInputStream.close();
                }
            } catch (IOException e) {
                LogUtil.d(e.getMessage());
            }
        }
        return null;
    }

    /**
     * checkBox的状态
     *
     * @param drawableCheckedId drawableCheckedId
     * @param drawableEmptyId drawableEmptyId
     * @param context context
     * @param checkbox checkbox
     */
    private static void checkBoxAddStatus(int drawableCheckedId, int drawableEmptyId,
                                          Context context, Checkbox checkbox) {
        StateElement stateElement = new StateElement();
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED},
                new PixelMapElement(getPixelMap(drawableCheckedId, context)));
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY},
                new PixelMapElement(getPixelMap(drawableEmptyId, context)));
        checkbox.setButtonElement(stateElement);
    }
}
